import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

public class FetchYXGTask implements Runnable{
    static AtomicInteger atomicInteger = new AtomicInteger(0);
    CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
    LinkedBlockingQueue<String> idpaths;
    public FetchYXGTask(LinkedBlockingQueue queue){
        idpaths=queue;
    }

    /**
     * 抓取  每个目录对应的   product_type_files
     * @param idPath
     * @throws IOException
     * @throws SQLException
     */
    public void fetch(String idPath) throws IOException, SQLException {
        if(org.apache.commons.lang.StringUtils.isEmpty(idPath))return;
        Integer i=atomicInteger.incrementAndGet();
        HttpGet httpGet = new HttpGet("https://www.yixuanpin.cn/api/supplier/goodsSuppliers?goodsId="+idPath+"&pageNum=1&pageSize=1000&sortBy=sale+-1");
        addHeader(httpGet);
        HttpResponse httpResponse = closeableHttpClient.execute(httpGet);
        String content = EntityUtils.toString(httpResponse.getEntity());
        try{
            Connection connection = JdbcUtil.getConnection();
            PreparedStatement pstmt = connection.prepareStatement("replace into wish_data_info (item_id,content) values(?,?)");
            pstmt.setString(1,idPath);
            pstmt.setString(2,content);
            pstmt.execute();
            JdbcUtil.releaseConn(connection);
            System.out.println("抓取第"+i+"个  idpath = "+idPath +"成功");
        }catch (Exception e){
            System.out.println(content);
            System.out.println("抓取第"+i+"个  idpath = "+idPath +"失败");
            e.printStackTrace();
        }


    }

    public static void addHeader(HttpGet httpPost){
        httpPost.addHeader("Host","www.yixuanpin.cn");
        httpPost.addHeader("Cookie","sails.sid=s%3AA1kVzZra915jJDEVc6_GsErDoX_3_5mK.TdG%2B5ByUluTJFXEuQP1%2B7coIH9Dz7QuvCxjnzD6np4w; user=%7B%22id%22%3A%22593fc60f341915cc15bdacb7%22%2C%22username%22%3A%22sorry%22%2C%22password%22%3A%22******%22%2C%22phone%22%3A%2215200920506%22%2C%22email%22%3A%22%22%2C%22companyName%22%3A%22%22%2C%22ownerInvitationCode%22%3A%22hVF9lz%22%2C%22loginCount%22%3A0%2C%22sessionTime%22%3A%222017-06-13%2019%3A01%3A35%22%7D; Hm_lvt_b067a69aef80b7d8a08f9f307667d672=1497344187; Hm_lpvt_b067a69aef80b7d8a08f9f307667d672=1497351778");
        httpPost.addHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
        httpPost.addHeader("Accept","*/*");
        httpPost.addHeader("Accept-Language","zh-CN,zh;q=0.8,en;q=0.6");

    }

    public void run() {
        String idpath;
        try {
            while((idpath=idpaths.take())!=null){
                fetch(idpath);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        LinkedBlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue<String>();
        List<String> strings = Files.readLines(new File("/Users/donar/Desktop/wish_itemId.txt"), Charsets.UTF_8);
        linkedBlockingQueue.addAll(strings);
        Executor executor = Executors.newFixedThreadPool(20);
        for (int i = 0; i < 20; i++) {
            executor.execute(new FetchYXGTask(linkedBlockingQueue));
        }
    }
}
